package com.fast.fast.common.gateway.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fast.fast.common.base.vo.Result;
import com.fast.fast.common.gateway.entity.SysPost;
import com.fast.fast.common.gateway.entity.SysUser;
import com.fast.fast.common.gateway.entity.SysUserPost;
import com.fast.fast.common.gateway.mapper.SysUserMapper;
import com.fast.fast.common.gateway.service.SysPostService;
import com.fast.fast.common.gateway.service.SysUserPostService;
import com.fast.fast.common.gateway.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lyf
 * @description 针对表【sys_user(系统用户表)】的数据库操作Service实现
 * @createDate 2022/01/01 00:00 周六
 */
@RequiredArgsConstructor
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {

    private final SysUserPostService sysUserPostService;

    private final SysPostService sysPostService;

    @Override
    public Result<SysUser> getByUsernameAndPwd(SysUser sysUser) {
        SysUser one = getBaseMapper().getByUsernameAndPwd(sysUser);
        if (ObjectUtil.isEmpty(one)) {
            return Result.error("用户名或密码错误");
        }
        List<Long> postIds = sysUserPostService.list(Wrappers.lambdaQuery(SysUserPost.class)
                .eq(SysUserPost::getUserId, one.getId())
        ).stream().map(SysUserPost::getPostId).collect(Collectors.toList());
        if (ObjectUtil.isNotEmpty(postIds)) {
            List<String> postNames = sysPostService.list(Wrappers.lambdaQuery(SysPost.class)
                    .in(SysPost::getId, postIds)
            ).stream().map(SysPost::getName).collect(Collectors.toList());
            one.setPostNames(postNames);
        }
        return Result.ok(one);
    }

}




